// ActionScript file
import com.model.DemoData;
import com.model.Model;
import com.services.GetBooksService;
import com.services.GetCoursesService;
import com.services.LoginService;

import mx.core.Application;
import mx.utils.URLUtil;

private var _app:Model = Model.getIntstance();
private var _booksService:GetBooksService = null;

private function login():void {
	if(Application.application.useDemoData) {
		handleLogin();
	}else {
		btn_login.enabled = false;
		//var loginService:LoginService = new LoginService(txt_username.text, txt_password.text, true, handleLogin);
		handleLogin();
	}
	// We should add some thing here untile the book created
}

private function handleLogin():void {
	Application.application.mainViewStack.selectedChild = Application.application.bookView;
	if(Application.application.useDemoData) {
		callLater(DemoData.setup);
	}else {
		// the following two lines should be removed after implementing the login
		
		_booksService = new GetBooksService(IslamicBook.userId, true, handleGettingBooks);
	}
}

private function handleGettingBooks():void {
	_app.books = _booksService.books;
	if(_app.books.length > 0) {
//		Application.application.mainViewStack.selectedChild = Application.application.bookView;
		_app.currentBook = _app.books[0];
	}
	if(_booksService.books.length > 1) {
		Application.application.bookView.addBooksComboBox();
	}
}

private function onCreationComplete():void {
	IslamicBook.BACKEND_URL =  URLUtil.getProtocol(Application.application.url);
	IslamicBook.BACKEND_URL += "://";
	IslamicBook.BACKEND_URL +=  URLUtil.getServerName(Application.application.url);
	var port:Number = URLUtil.getPort(Application.application.url);
	if( port != 0)
	{
		IslamicBook.BACKEND_URL += ":";
		IslamicBook.BACKEND_URL +=  port.toString();
	}
	IslamicBook.BACKEND_URL += "/";
}

